package com.sinosoft.service;

import org.activiti.engine.*;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.history.HistoricVariableInstance;
import org.activiti.engine.impl.persistence.entity.VariableInstance;

import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by oracle on 2017-03-11.
 */
@Service
@ConfigurationProperties(prefix = "activiti.process")
public class ActivitiServiceImpl implements ActivitiService {
    //    @Autowired
//    ProcessEngineConfiguration processEngineConfiguration;
    @Autowired
    TaskService taskService;

    //    @Autowired
//    RepositoryService repositoryService;
    @Autowired
    RuntimeService runtimeService;
    @Autowired
    HistoryService historyService;

    String proccessName;//工作流名称
    List<String> assignee;  //处理人角色
    String organization;   //组织机构

    @Override
    public void testActivity() {
    /*    List<Deployment> deployments = repositoryService.createDeploymentQuery().deploymentName(proccessName).list();
        for (Deployment deployment : deployments) {
            String name = deployment.getName();
            System.out.println("name = " + name);
        }*/
        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("day", 5);
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(proccessName, organization, variables);

        //第一个节点
        Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).taskAssignee(assignee.get(0)).processInstanceBusinessKey(organization).singleResult();
        // TODO: 2017-03-12 do something
        taskService.complete(task.getId());
        //第二个节点
        task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).taskAssignee(assignee.get(2)).processInstanceBusinessKey(organization).singleResult();
        taskService.complete(task.getId());
        //第三个节点
        task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).taskAssignee(assignee.get(3)).processInstanceBusinessKey(organization).singleResult();
        taskService.complete(task.getId());
//        task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
//        taskService.complete(task.getId());
//        boolean ended = processInstance.isEnded();
    }

    @Override
    public void startProccess() {
        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("day", 5);
        variables.put("products", "一台电脑，一台空调");
        variables.put("time", new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(new Date()));
        variables.put("organization", organization);
        runtimeService.startProcessInstanceByKey(proccessName, organization, variables);
    }

    @Override
    public void handleProccess() {
    /*    List<Execution> executions = runtimeService.createExecutionQuery().processDefinitionKey(proccessName).list();
        for (Execution execution : executions) {
            Map<String, Object> variables = taskService.getVariables(execution.getId());
            for (Map.Entry<String, Object> stringObjectEntry : variables.entrySet()) {
                System.out.println(stringObjectEntry.getKey() + "  " + stringObjectEntry.getValue());
            }
        }*/
        List<ProcessInstance> processInstances = runtimeService.createProcessInstanceQuery().processDefinitionKey(proccessName).processInstanceBusinessKey(organization).list();
        for (ProcessInstance processInstance : processInstances) {
            Map<String, VariableInstance> variableInstances = runtimeService.getVariableInstances(processInstance.getId());
            for (Map.Entry<String, VariableInstance> stringVariableInstanceEntry : variableInstances.entrySet()) {
                System.out.println(stringVariableInstanceEntry.getKey() + "  " + stringVariableInstanceEntry.getValue().getValue());
            }

            Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).processInstanceBusinessKey(organization).singleResult();
            taskService.complete(task.getId(), Collections.<String, Object>singletonMap("result", "同意"));
            System.out.println("processInstance.isEnd = " + processInstance.isEnded());
        }
    }

    @Override
    public Boolean isend() {
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processDefinitionKey(proccessName).singleResult();
        return processInstance.isEnded();
    }

    @Override
    public void history() {
        List<HistoricProcessInstance> historicProcessInstances = historyService.createHistoricProcessInstanceQuery().processDefinitionKey(proccessName).processInstanceBusinessKey(organization).list();
        for (HistoricProcessInstance historicProcessInstance : historicProcessInstances) {
            List<HistoricVariableInstance> historicVariableInstances = historyService.createHistoricVariableInstanceQuery().processInstanceId(historicProcessInstance.getId()).list();
            for (HistoricVariableInstance historicVariableInstance : historicVariableInstances) {
                System.out.println(historicVariableInstance.getVariableName() +"  "+historicVariableInstance.getValue());
            }
            List<HistoricTaskInstance> historicTaskInstances = historyService.createHistoricTaskInstanceQuery().processInstanceId(historicProcessInstance.getId()).processInstanceBusinessKey(organization).list();
            for (HistoricTaskInstance historicTaskInstance : historicTaskInstances) {
                Map<String, Object> processVariables = historicTaskInstance.getProcessVariables();
                List<HistoricVariableInstance> variableInstances = historyService.createHistoricVariableInstanceQuery().taskId(historicTaskInstance.getId()).list();
                for (HistoricVariableInstance variableInstance : variableInstances) {
                    System.out.println(variableInstance.getVariableName() + "  " + variableInstance.getValue());
                }
            }

        }

    }

    public String getProccessName() {
        return proccessName;
    }

    public void setProccessName(String proccessName) {
        this.proccessName = proccessName;
    }

    public List<String> getAssignee() {
        return assignee;
    }

    public void setAssignee(List<String> assignee) {
        this.assignee = assignee;
    }

    public String getOrganization() {
        return organization;
    }

    public void setOrganization(String organization) {
        this.organization = organization;
    }
}
